/**
 * Created by Trialiet on 2016/9/1.
 */
$(document).ready(function () {
    $.getJSON("initMenu.action", function (data) {
        $.each(data.result, function (index, object) {
            showMenu(object);
        });
    });
})

function showMenu(o) {
    $("#menu").append("<tr id='menu" + o.id + "'>" +
        "<td align='center'>" + o.name + "</td>" +
        "<td align='center'>" + o.price + "</td>" +
        "</tr>");
}

function addDish() {
    var name = $("#name").val();
    var price = parseInt($("#price").val());
    var jsonArray = [];
    var json = $.parseJSON('{"name" : \"' + name + '\", "price" : ' + price + '}');
    jsonArray.push(json);
    var data = JSON.stringify(jsonArray);
    $.ajax({
        type: "post",
        url: "addNewDish.action",
        data: "{\"data\": " + data + "}",
        contentType: "application/json;charset=utf-8",
        dataType: 'json',
        async: false,
        complete: function () {
            alert("添加成功！");
            window.location.reload();
        }
    });
}